﻿Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports NUnit.Framework
Imports System.Diagnostics

Namespace Axial.Tests
    <JSSafe()> _
    Public Class SafeClass
        Public Sub New()
            Prop = 0
            StaticField = 3.0R
        End Sub
        Public Sub New(ByVal i As Integer)
            Prop = i
        End Sub
        Public Sub New(ByVal s As String)
            Field = s
        End Sub

        Private Shared _StaticField As Double
        Public Shared Property StaticField() As Double
            Get
                Return _StaticField
            End Get
            Set(ByVal value As Double)
                _StaticField = value
            End Set
        End Property
        Public Field As String = "Initialized"
        Private _Prop As Integer
        Public Property Prop() As Integer
            Get
                Return _Prop
            End Get
            Set(ByVal value As Integer)
                _Prop = value
            End Set
        End Property

        Public Sub VoidMethod()
            Prop = 2
        End Sub

        Public Function FancyMethod(ByVal arg0 As String) As String
            Return arg0 & "done"
        End Function

        Public Shared Function StaticMethod() As Double
            Return StaticField
        End Function
    End Class

    <TestFixture()> _
    Public Class ClassTests
        Inherits TestBase
        <Test()> _
        Public Sub SafeClassTest()
            Dim check As String = _
"function SafeClass()" & vbNewLine & _
"{" & vbNewLine & _
"	this.Field = null;" & vbNewLine & _
"	var _Prop = 0;" & vbNewLine & _
"	this.constructor._StaticField = 0;" & vbNewLine & _
"	this.get_Prop = function()" & vbNewLine & _
"	{" & vbNewLine & _
"		return this._Prop;" & vbNewLine & _
"	}" & vbNewLine & _
"	this.set_Prop = function(arg0)" & vbNewLine & _
"	{" & vbNewLine & _
"		this._Prop = arg0;" & vbNewLine & _
"	}" & vbNewLine & _
"	this.VoidMethod = function()" & vbNewLine & _
"	{" & vbNewLine & _
"		this.set_Prop(2);" & vbNewLine & _
"	}" & vbNewLine & _
"	this.FancyMethod = function(arg0)" & vbNewLine & _
"	{" & vbNewLine & _
"		return (arg0) + ('done');" & vbNewLine & _
"	}" & vbNewLine & _
"	this.get_StaticField = function()" & vbNewLine & _
"	{" & vbNewLine & _
"		return this.constructor._StaticField;" & vbNewLine & _
"	}" & vbNewLine & _
"	this.set_StaticField = function(arg0)" & vbNewLine & _
"	{" & vbNewLine & _
"		this.constructor._StaticField = arg0;" & vbNewLine & _
"	}" & vbNewLine & _
"	this.StaticMethod = function()" & vbNewLine & _
"	{" & vbNewLine & _
"		return this.get_StaticField();" & vbNewLine & _
"	}" & vbNewLine & _
vbTab & vbNewLine & _
"	if (arguments.length == 0)" & vbNewLine & _
"	{" & vbNewLine & _
"		this.Field = 'Initialized';" & vbNewLine & _
"		this.set_Prop(0);" & vbNewLine & _
"		this.set_StaticField(3);" & vbNewLine & _
"	}" & vbNewLine & _
"	if (arguments.length == 1 && typeof arguments[0] == ""number"")" & vbNewLine & _
"	{" & vbNewLine & _
"		this.Field = 'Initialized';" & vbNewLine & _
"		this.set_Prop(arguments[0]);" & vbNewLine & _
"	}" & vbNewLine & _
"	if (arguments.length == 1 && typeof arguments[0] == ""string"")" & vbNewLine & _
"	{" & vbNewLine & _
"		this.Field = 'Initialized';" & vbNewLine & _
"		this.Field = arguments[0];" & vbNewLine & _
"	}" & vbNewLine & _
"}"

            Dim code As String = JSWebTranslator.DissembleClass(GetType(SafeClass))
            code = code.Trim()
            ' remove ending newline 
            Trace.WriteLine("SafeClass class")
            Trace.WriteLine("------------------------------------")
            Trace.WriteLine(code)
            Trace.WriteLine("")
            Assert.AreEqual(check, code.Substring(code.Length - check.Length, check.Length))
        End Sub

        <Test()> _
        Public Sub ClassUsageTest()
            VerifyMethodOutput("ClassUsage", _
"a = new SafeClass();" & vbNewLine & _
"b = new SafeClass(3);" & vbNewLine & _
"c = new SafeClass('a');" & vbNewLine & _
"a.FancyMethod('arg0');" & vbNewLine & _
"b.Field = 'Value';" & vbNewLine & _
"c.set_Prop(3);" & vbNewLine & _
"a.VoidMethod();")
        End Sub

        Public Sub ClassUsage()
            Dim a = New SafeClass()
            Dim b = New SafeClass(3)
            Dim c = New SafeClass("a")

            a.FancyMethod("arg0")
            b.Field = "Value"
            c.Prop = 3
            a.VoidMethod()
        End Sub
    End Class
End Namespace